package com.zhongxi.guoyuan.adapter.personCenter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.center.InnerOrderResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2017/1/19.
 */

public class InnerOrderAdapter extends BaseAdapter {

    private Context mContext;
    private List<InnerOrderResult.InnerOrderData> list;

    public InnerOrderAdapter(Context context, List<InnerOrderResult.InnerOrderData> data) {
        this.mContext = context;
        this.list = data;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try{
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_inner_order, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.tvOrderId = (TextView) convertView.findViewById(R.id.tvOrderId);
                holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvType = (TextView) convertView.findViewById(R.id.tvType);
                holder.tvCash = (TextView) convertView.findViewById(R.id.tvCash);
                holder.tvReward = (TextView) convertView.findViewById(R.id.tvReward);
                holder.ivDel = (ImageView) convertView.findViewById(R.id.ivDel);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            try{
                if(null!=list.get(position)&&!"".equals(list.get(position))){
                    final InnerOrderResult.InnerOrderData data = list.get(position);
                    if(null!=data.getServiceimg()&&!"".equals(data.getServiceimg())){
                        Glide.with(mContext).load(Url.IMG_URL+data.getServiceimg()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(mContext, 20)).into(holder.ivImg);
                    }
                    if(null!=data.getId()&&!"".equals(data.getId())){
                        holder.tvOrderId.setText("订单号码："+data.getId());
                    }else{
                        holder.tvOrderId.setText("订单号码：");
                    }
                    if(null!=data.getCtime()&&!"".equals(data.getCtime())){
                        holder.tvTime.setText("完成时间："+data.getCtime());
                    }else{
                        holder.tvTime.setText("完成时间：");
                    }
                    if(null!=data.getServicename()&&!"".equals(data.getServicename())){
                        holder.tvName.setText("项目名称："+data.getServicename());
                    }else{
                        holder.tvName.setText("项目名称：");
                    }
                    if(null!=data.getSstype()&&!"".equals(data.getSstype())){
                        if("1".equals(data.getSstype())){
                            holder.tvType.setText("服务方式：上门服务");
                        }else{
                            holder.tvType.setText("服务方式：到店服务");
                        }
                    }
                    String money="";
                    if(null!=data.getPayinnerprice()&&!"".equals(data.getPayinnerprice())){
                        money+=data.getPayinnerprice()+"美币";
                    }else{
                        money+="*美币";
                    }
                    if(null!=data.getPay()&&!"".equals(data.getPay())&&!"".equals(data.getBacksilvernum())){
                        money+=data.getPay()+"元";
                        holder.tvReward.setText("[返银币"+new BigDecimal(data.getPay()).multiply(new BigDecimal(data.getBacksilverratio())).divide(new BigDecimal(100)).intValue()+"银币]");
                    }else{
                        money+="*元";
                        holder.tvReward.setText("[返银币*银币]");
                    }
                    if(data.getSelect()){
                        holder.ivDel.setImageResource(R.mipmap.iv_select);
                    }else{
                        holder.ivDel.setImageResource(R.mipmap.iv_noselect);
                    }
                    holder.tvCash.setText(money);
                    //删除选中
                    holder.ivDel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(data.getSelect()){
                                data.setSelect(false);
                            }else{
                                data.setSelect(true);
                            }
                            notifyDataSetChanged();
                        }
                    });
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    public String getOrderids() {
        String str = "";
        for(int i=0;i<list.size();i++){
            if(list.get(i).getSelect())
                str += list.get(i).getId()+",";
        }
        return str.substring(0, str.length()-1);
    }

    class ViewHolder{
        private ImageView ivImg;
        private TextView tvOrderId;
        private TextView tvTime;
        private TextView tvName;
        private TextView tvType;
        private TextView tvCash;
        private TextView tvReward;
        private ImageView ivDel;
    }
}
